package com.qisi.kandroidshalon.permission

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

object PermissionManager {
    private val TAG = "PermissionManager"

    private val permissions = arrayOf(
        Manifest.permission.INTERNET
    )

    /**
     * 检查权限
     */
    fun checkPermission(act: Activity): Boolean {
        var result = true
        for (p in permissions) {
            val b = ContextCompat.checkSelfPermission(
                act,
                p,
            ) == PackageManager.PERMISSION_GRANTED
            Log.i(TAG, "onRequestPermissionsResult：${p} granted: $b")
            result = b && result
            if (!result) {
                return false
            }
        }
        return result
    }

    /**
     * 请求权限
     */
    fun requestPermissions(act: Activity) {
        val permissions = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )
        ActivityCompat.requestPermissions(act, permissions, 1)
    }
}